//
//  LiveStream.swift
//  App
//
//  Created by melvyn on 2018/8/9.
//

import Vapor
import Fluent
import Pagination

struct LiveStream: Codable {
    
    var id: Int?
    var uid: User.ID
    var key: String
    var title: String
    var thumbnail: String?
    var tag: String?
    var numberOfVisits: Int?
    var status: Int?
}

extension LiveStream: Entity {}
extension LiveStream: Migration {}
extension LiveStream: Content {}
extension LiveStream: Parameter {}
extension LiveStream: Paginatable {}

extension LiveStream {
    var up: Parent<LiveStream, User> {
        return parent(\.uid)
    }
    
    var comments: Children<LiveStream, Comment> {
        return children(\.liveId)
    }
}

extension LiveStream {
    
    struct FormData: Codable {
        var id: Int?
        var uid: User.ID?
        var key: String
        var title: String
        var tag: String?
        var thumbnail: String?
        var metadata: Data?
    }
}

extension LiveStream.FormData: Entity {
    static var entity = LiveStream.entity
}

extension LiveStream.FormData: Content {}
extension LiveStream.FormData: Parameter {}


extension LiveStream {
    
    struct Public: Codable {
        var id: Int?
        var key: String
        var title: String
        var thumbnail: String?
        var tag: String?
        var numberOfVisits: Int?
        var status: Int?
        var upload: User.Public?
        var uid: User.ID?
        var comments: [Comment]?
        
        init(liveStream: LiveStream, upload: User.Public? = nil, comments: [Comment]? = nil) {
            id = liveStream.id
            key = liveStream.key
            title = liveStream.title
            thumbnail = liveStream.thumbnail
            tag = liveStream.tag
            numberOfVisits = liveStream.numberOfVisits
            status = liveStream.status
            uid = liveStream.uid
            self.upload = upload
            self.comments = comments
        }
    }
}

extension LiveStream.Public: Content {}
